<?php

namespace App\HttpController;


use App\Model\t_s_app;
use App\Traits\TraitExcel;
use App\X\XList;
use App\X\XWebClient\WebClient;
use EasySwoole\Component\Openssl;
use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\ServerManager;

class Test extends ControllerBase
{
    use TraitExcel;


    function test_intval()
    {
        $this->commonR()->data["intval('123')"] = intval('123');
        $this->commonR()->data["intval('adsfsdf')"] = intval('adsfsdf');

        $this->funcWriteCommonR();
    }

    function index()
    {
        $x = $this->db()->getOne(t_s_app::table());

        $worker_id = \EasySwoole\EasySwoole\ServerManager::getInstance()
            ->getSwooleServer()->worker_id;
        var_dump($worker_id);
        go(function ()
        {
            \co::sleep(1);
            $worker_id = \EasySwoole\EasySwoole\ServerManager::getInstance()
                ->getSwooleServer()->worker_id;
            var_dump($worker_id);

//            $x = $this->db()->getOne(t_s_app::table());

            defer(function ()
            {
//                $this->context()->destroy();
            });
        });

        go(function ()
        {
            $worker_id = \EasySwoole\EasySwoole\ServerManager::getInstance()
                ->getSwooleServer()->worker_id;
            var_dump($worker_id);

            \co::sleep(2);
//            $x = $this->db()->getOne(t_s_app::table());

            defer(function ()
            {
//                $this->context()->destroy();
            });
        });
//
//
//        go(function ()
//        {
//            $this->redis()->incr('test');
//        });
//
//        go(function ()
//        {
//            $this->redis()->incr('tes1');
//        });

        $worker_id = \EasySwoole\EasySwoole\ServerManager::getInstance()
            ->getSwooleServer()->worker_id;
        var_dump($worker_id);
        $this->funcWriteCommonR();

//        \h::funcLog(__METHOD__, 'test');
//        \h::funcLog(__METHOD__, 'test');
//        \h::funcLog(__METHOD__, 'test');
//        \h::funcLog(__METHOD__, 'test');
//        \h::funcLog(__METHOD__, 'test');

//        $table_name = 'wx_app.t_t_test';
//
//        $arr_first = $this->db()
//            ->where('t_create_date', \h::today()->toDateString(), '>')
//            ->getOne($table_name);
//        var_dump($arr_first['t_create_date']);
//
//        $x = $this->redis()->get('123');
//        var_dump($x);
//        return;
//
//        for ($i = 0; $i < 100; $i++)
//        {
//            $s = $this->db()->rawQuery('SELECT 1');
//            var_dump($s);
//        }
//        return;
//        $client = new WebClient("http://admin.wx.chimelong.com/z_admin/index.aspx?site_id=chimelong");
//        $res = $client
////            ->addHeader('Content-Type', 'text/html;charset=utf-8')
//            ->get();
//
//        $this->response()->withHeader('Content-Type', 'text/html;charset=utf-8');
//        $this->response()->write($res->getBody());
//        var_dump($res->getBody());

//        $list = $this->db()->get(t_s_app::table());
//        $arr = [
//            'abc_id' => '1',
//            'abc_site_id' => '1',
//            'abc_table' => '1',
//            'abc_pid' => '1',
//            'abc_order' => '1',
//        ];
//
//        $arr1 = [
//            'abc_id' => '2',
//            'abc_site_id' => '2',
//            'abc_table' => '2',
//            'abc_pid' => '2',
//            'abc_order' => '2',
//        ];
//
//        $list = [$arr, $arr1];
//
//        $a = XArray::funcIns($list);
//
////        var_dump($a->where('abc_site_id', '2')->select('abc_id,abc_order'));
//        $list = $a->where('abc_site_idx', '2', function ($v1, $v2)
//        {
//            var_dump($v1, $v2);
//
//            return $v1 == $v2;
//        });
//        var_dump($list);
//
//
//        $this->response()->write('123');
    }

    function test()
    {
        for ($i = 0; $i <= 10; $i++)
        {
            $this->response()->getSwooleResponse()->write("V{$i}<br>");
            sleep(1);
        }

        $this->response()->end();
    }

    function test_excel()
    {
        $this->ExportCsvHead();


        $head = [
            'a' => 'a1',
            'b' => 'b1',
            'c' => 'c1',
            'd' => 'd1',
            'e' => 'e1',
        ];


        $data = [];
        for ($i = 0; $i <= 10; $i++)
        {
            $row = [];
            foreach ($head as $key => $val)
            {
                $row[$key] = "val-{$key}-{$i}";
            }

            $data[] = $row;
        }


        $is_first = true;
        for ($i = 0; $i <= 10; $i++)
        {
            $data_e = $this->ExportSetData($head, $data, !$is_first);

            $is_first = false;


            $this->ExportCsvAddData($data_e);
        }
    }

    function test_jj()
    {
        $message = "你好，12312312312312313311";
        $data = [
            'msgtype' => 'text',
            'text' => ['content' => \Carbon::now()->toDateTimeString()]
        ];
        $post_str = json_encode($data);


        $url = 'https://oapi.dingtalk.com/robot/send?access_token=76dfe4556b07117276a9d72c856717d52c062ae3745083fc420c042ffcc8013b';
//
//        $client = new HttpClient($url);
////        var_dump($client);
//        $client->post($post_str, 'application/json;charset=utf-8');
//        $x = $client->exec();
//        $this->commonR()->data['return_post'] = $x->getBody();
//        var_dump($x->getBody());


        $client = new WebClient($url);
        $res = $client
            ->setHeader('Content-Type', 'application/json;charset=utf-8')
            ->post($post_str);
//        var_dump($res);
        $this->commonR()->data['return_post'] = $res->getBody();

//        $ch = curl_init();
//        $arr = [
//            CURLOPT_URL => $url,
//            CURLOPT_POST => true,
//            CURLOPT_CONNECTTIMEOUT => 5,
//            CURLOPT_HTTPHEADER => array('Content-Type: application/json;charset=utf-8'),
//            CURLOPT_POSTFIELDS => $post_str,
//            CURLOPT_RETURNTRANSFER => true,
//        ];
//
//        $arr = [
//            'Content-Type' => 'application/json;charset=utf-8',
//        ];
//
//        $x = \h::XArray($arr)->toBuildQuery(':');
//        var_dump($x);
//        var_dump($arr);
//        curl_setopt_array($ch, $arr);
//        curl_setopt($ch, CURLOPT_URL, $url);
//        curl_setopt($ch, CURLOPT_POST, 1);
//        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
//        $data = curl_exec($ch);
//        var_dump($data);
//
//        curl_close($ch);

//        $this->commonR()->data['return_post'] = $data;

        $this->funcWriteCommonR();
    }


    public function test_task()
    {
        var_dump('begin=' . \h::now()->toDateTimeString());
        Timer::getInstance()->after(2 * 1000, function ()
        {
            Timer::getInstance()->loop(2 * 1000, function ()
            {
                $w_id = ServerManager::getInstance()->getSwooleServer()->worker_id;

                var_dump($w_id . 'loop=' . \h::now()->toDateTimeString());
            });

            $w_id = ServerManager::getInstance()->getSwooleServer()->worker_id;
            var_dump($w_id . 'after=' . \h::now()->toDateTimeString());
        });
    }

    function test_redis()
    {
        $this->redis()->set('aaa', '123');
    }

    function test_session()
    {
        $session = $this->xsession();
//
        $session->start();

        $session->set('xxx', \h::funcGetID());
        $x = $session->get('xxx');
        var_dump($session->sid());
//        $session->destroy();
        $session->writeClose();
        $this->commonR()->data = $x;

        return $this->funcWriteCommonR();
//        $this->sessionDriver('x');
//        $this->sessionDriver()
//        ini_set('session.save_handler', 'redis');
//        ini_set('session.save_path', 'tcp://192.168.0.88:6379');
//
//        session_start();
//        $_SESSION['sessionid'] = \h::funcGetID();

    }


    function test_openssl()
    {
        $openssl = new Openssl(\h::funcKey('x'));

        $this->commonR()->data['enc'] = $openssl->encrypt('1');


        return $this->funcWriteCommonR();
    }

}